package hy;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class EsIndex {
    RestHighLevelClient cli;
    @Before
    public void init() {
        cli=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.188.100",9200,"http"))
        );
    }
    @After
    public void end()throws Exception{
        cli.close();
    }
    @Test//创建索引
    public void createIndex()throws Exception{
        //创建索引请求
        CreateIndexRequest req=new CreateIndexRequest("user");
        //创建索引
        CreateIndexResponse resp = cli.indices().create(req, RequestOptions.DEFAULT);
        //响应状态
        boolean ack = resp.isAcknowledged();
        System.out.println("索引操作状态："+ack);
    }
    @Test
    public void searchIndex()throws Exception{
        GetIndexRequest req = new GetIndexRequest("user");
        //创建索引
        GetIndexResponse resp = cli.indices().get(req, RequestOptions.DEFAULT);
        //响应状态
        System.out.println(resp.getAliases());
        System.out.println(resp.getSettings());
    }
    @Test
    public void deleteIndex()throws Exception{
        DeleteIndexRequest req =new DeleteIndexRequest("user");
        AcknowledgedResponse resp = cli.indices().delete(req, RequestOptions.DEFAULT);
        System.out.println(resp.isAcknowledged());
    }
}